package com.trgzs.bate.common.response;

import com.trgzs.bate.common.enums.IBaseResultEnum;
import com.trgzs.bate.common.enums.BaseResultEnum;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @Author: dongl
 * @Email: dong980514280@gmail.com
 * @Create: 2025-02-28 14:24:15
 * @PS
 **/
@Data
@Accessors(chain = true)
public class BaseResult<T> {

    /**
     * 返回码
     */
    public int code;

    /**
     * 返回信息
     */
    public String msg;

    /**
     * 返回数据
     */
    public T data;

    /**
     * 执行时间（毫秒）
     */
    public long durationMs;

    private BaseResult() {}

    public BaseResult(int code, String message) {
        this.code = code;
        this.msg = message;
    }

    public BaseResult(IBaseResultEnum baseResultEnum) {
        this.code = baseResultEnum.getCode();
        this.msg = baseResultEnum.getMessage();
    }

    public BaseResult(int code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }

    public BaseResult(IBaseResultEnum baseResultEnum, T data) {
        this.code = baseResultEnum.getCode();
        this.msg = baseResultEnum.getMessage();
        this.data = data;
    }


    public static  BaseResult<?> success() {
        return new BaseResult<>(BaseResultEnum.SUCCESS);
    }

    /**
     * 默认形式，无返回数据成功
     * @return BaseResult
     */
    public static <T> BaseResult<T> success(Class<T> clazz) {
        return new BaseResult<>(BaseResultEnum.SUCCESS);
    }

    /**
     * 附带数据的成功
     * @param data 数据
     * @return BaseResult
     */
    public static <T> BaseResult<T> success(T data) {
        return new BaseResult<>(BaseResultEnum.SUCCESS, data);
    }

    public static  BaseResult<?> fail() {
        return new BaseResult<>(BaseResultEnum.FAILED);
    }

    /**
     * 默认形式，无返回数据失败
     * @return BaseResult
     */
    public static <T> BaseResult<T> fail(Class<T> clazz) {
        return new BaseResult<>(BaseResultEnum.FAILED);
    }

    /**
     * 携带信息，返回失败
     * @param message 失败信息
     * @return BaseResult
     */
    public static <T> BaseResult<T> fail(String message) {
        return new BaseResult<>(BaseResultEnum.FAILED.getCode(), message);
    }


    /**
     * 携带信息，返回失败
     * @param code 失败代码
     * @param message 失败信息
     * @return BaseResult
     */
    public static <T> BaseResult<T> fail(int code, String message) {
        return new BaseResult<>(code, message);
    }

}
